package com.wx.task;

import lombok.extern.slf4j.Slf4j;

/**
 * 线程历史1
 *
 * @author wang.xun
 * @date 2023/07/24
 */
@Slf4j
public class ThreadHistory1 implements Runnable{
    boolean flag = true;
    @Override
    public void run() {

        while (true) {
            if(getShutdown()){
                doSomeThing();

            }else{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    private boolean getShutdown() {
        return flag;
    }

    private void shutdown(boolean flag) {
        this.flag = flag;
    }

    private void doSomeThing() {
        log.info("业务计算");
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        ThreadHistory1 history1 = new ThreadHistory1();
        new Thread(history1).start();
        try {
            // 5秒后，停止执行
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("停止业务计算");
        history1.shutdown(false);

    }
}
